package login;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * Servlet implementation class UpdateAccountServlet
 */
@WebServlet("/UpdateAccountServlet")
public class UpdateAccountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateAccountServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer userID = Integer.parseInt(request.getParameter("userID"));
        String firstName = request.getParameter("fname");
        String lastName = request.getParameter("lname");
        String phoneNumber = request.getParameter("phone");
        String emailAddress = request.getParameter("email");

        String updateUser = "update user set FName = '" + firstName + "', LName = '" + lastName + "', Phone = '"
        + phoneNumber + "', Email = '" + emailAddress + "' where UserID = " + userID ;

        try {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        }
        try {
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/defect_tracker","root", "root");
            Statement stmt = con.createStatement();
            stmt.executeUpdate(updateUser);

         // redirect to success page
            response.sendRedirect("EditAccount.jsp");



        }    catch (SQLException e) {
                System.out.println("SQLException occured: " + e.getMessage());
                e.printStackTrace();
            }
	}
}
